package com.roc.live.util;

import android.app.ActivityManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.roc.live.LiveApplication;
import com.roc.live.model.NetworkType;

import java.util.ArrayList;

public class LiveUtil {
    /**
     * 判断服务是否开启
     *
     * @return boolean
     */
    public static boolean isServiceRunning(Context context, String ServiceName) {
        if (StringUtil.isEmpty(ServiceName))
            return false;
        ActivityManager myManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager
                .getRunningServices(Integer.MAX_VALUE);
        if (runningService != null) {
            for (int i = 0; i < runningService.size(); i++) {
                String name = runningService.get(i).service.getShortClassName();
                String fullName = runningService.get(i).service.getClassName();
                if (fullName.equals(ServiceName)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static NetworkType getNetworkType() {
        Context context = LiveApplication.getAppContext();
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null) {
            NetworkType networkType;
            if (networkInfo.isConnected()) {
                int type = networkInfo.getType();
                switch (type) {
                    case ConnectivityManager.TYPE_WIFI:
                        networkType = NetworkType.WIFI;
                        break;
                    case ConnectivityManager.TYPE_MOBILE:
                    case ConnectivityManager.TYPE_MOBILE_DUN:
                    case ConnectivityManager.TYPE_MOBILE_HIPRI:
                    case ConnectivityManager.TYPE_MOBILE_MMS:
                    case ConnectivityManager.TYPE_MOBILE_SUPL:
                        networkType = NetworkType.MOBILE;
                        break;
                    default:
                        networkType = NetworkType.UNKNOWN;
                        break;
                }
                return networkType;
            }
        }
        return NetworkType.NotConnected;
    }
}
